package ws_vecirek;

import java.io.File;

import com.sleepycat.dbxml.XmlManager;
import com.sleepycat.dbxml.XmlContainer;
import com.sleepycat.dbxml.XmlQueryContext;
import com.sleepycat.dbxml.XmlQueryExpression;
import com.sleepycat.dbxml.XmlResults;
import com.sleepycat.dbxml.XmlValue;

public class ProVecirekService {
	
	final String containerName = "partyNativDB.dbxml"; 
	final File path2DbEnv = new File("C:/aa");
	
	// This function is used to ensure that databases are
    // properly closed, even on exceptions
    private static void cleanup(MyDbEnv env, XmlContainer openedContainer) {
    	try {
    	    if (openedContainer != null)
    	    	openedContainer.delete();
    	    if (env != null)
    	    	env.cleanup();
    	} catch (Exception e) {
    	    // ignore exceptions on close
    	}
    }
	
	public String insertParty( int day, int month, int year, int hour, int minute, 
			int idKapela, int idCatering, int idBudova, int ico
			){
		String content = "<party>" +
					"<ico>" + ico + "</ico>" +
					"<year>" + year + "</year>" +
					"<month>" + month + "</month>" +
					"<day>" + day + "</day>" +
					"<hour>" + hour + "</hour>" +
					"<minute>" + minute + "</minute>" +
					"<kapela>" + idKapela + "</kapela>" +
					"<catering>" + idCatering + "</catering>" +
					"<budova>" + idBudova + "</budova>" +
				"</party>";
		String docName = "doc" + content.hashCode();
		MyDbEnv env = null;
		XmlManager mgr = null;
		XmlContainer cont = null;
		try {
			// All BDB XML programs require an XmlManager instance
			env = new MyDbEnv(path2DbEnv);
			mgr = env.getManager();
			if (mgr.existsContainer(containerName) != 0){
				cont = mgr.openContainer(containerName);
			} else {
				cont = mgr.createContainer(containerName);
			}
			cont.putDocument(docName, content);
			
			} catch (Exception xe) {
			return "<exception>XmlException during insertParty: " + xe.getMessage() + "</exception>";
		}
		finally {
			cleanup(env, cont);
		}
		
		return "<insert>OK</insert>";
	}
	
	public String getPartyByKapela(int idKapela){
		StringBuffer result = new StringBuffer();
		MyDbEnv env = null;
		XmlManager mgr = null;
		XmlContainer cont = null;
		String queryString = "collection('" + containerName + "')/party[kapela = $idKapela]";
		try {
		    // All BDB XML programs require an XmlManager instance
			env = new MyDbEnv(path2DbEnv);
			mgr = env.getManager();
		    
		    if (mgr.existsContainer(containerName) != 0){
		    	cont = mgr.openContainer(containerName);
		    } else {
		    	cont = mgr.createContainer(containerName);
		    }
		    
		    // Querying requires an XmlQueryContext
		    XmlQueryContext qc = mgr.createQueryContext();
		    
		    // Add a variable to the query context, used by the query
		    qc.setVariableValue("idKapela", new XmlValue(idKapela));
		    
		    // Note: these two calls could be replaced by a shortcut
		    // mgr.query(queryString, qc), but under the covers,
		    // this is what is happening.
		    XmlQueryExpression expr = mgr.prepare(queryString, qc);
		    XmlResults res = expr.execute(qc);		    
		    
		    XmlValue value = new XmlValue();
		    result.append("<data>");
		    while ((value = res.next()) != null) {
		    	result.append(value.asString());
			}
		    result.append("</data>");
		} catch (Exception xe) {
			return "<exception>XmlException during insertParty: " + xe.getMessage() + "</exception>";
		}
		finally {
		    cleanup(env, cont);
		}
		
		
		return result.toString();
	}
	
	public String getPartyAll(){
		StringBuffer result = new StringBuffer();
		MyDbEnv env = null;
		XmlManager mgr = null;
		XmlContainer cont = null;
		String queryString = "collection('" + containerName + "')/party";
		try {
		    // All BDB XML programs require an XmlManager instance
			env = new MyDbEnv(path2DbEnv);
			mgr = env.getManager();
		    
		    if (mgr.existsContainer(containerName) != 0){
		    	cont = mgr.openContainer(containerName);
		    } else {
		    	cont = mgr.createContainer(containerName);
		    }
		    
		    // Querying requires an XmlQueryContext
		    XmlQueryContext qc = mgr.createQueryContext();
		   
		    // Note: these two calls could be replaced by a shortcut
		    // mgr.query(queryString, qc), but under the covers,
		    // this is what is happening.
		    XmlQueryExpression expr = mgr.prepare(queryString, qc);
		    XmlResults res = expr.execute(qc);		    
		    
		    XmlValue value = new XmlValue();
		    result.append("<data>");
		    while ((value = res.next()) != null) {
		    	result.append(value.asString());
			}
		    res.delete();
		    expr.delete();
		    result.append("</data>");
		} catch (Exception xe) {
			return "<exception>XmlException during insertParty: " + xe.getMessage() + "</exception>";
		}
		finally {
			cleanup(env, cont);
		}
		
		
		return result.toString();
	}
}
